﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.ServiceModel.Activation
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Public Class ServiceFactory
  Inherits ServiceHostFactory
  Protected Overloads Overrides Function CreateServiceHost(ByVal serviceType As Type, ByVal baseAddresses As Uri()) As ServiceHost
    Dim host = MyBase.CreateServiceHost(serviceType, baseAddresses)
    host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.[Custom]
    host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = New UserNameValidator()

    Dim compression = New Albatross.WCF.Extenions.ServiceModel.Channels.CompressionBindingElement()
    compression.Algorithm = Albatross.WCF.Extenions.ServiceModel.Channels.CompressionAlgorithm.GZip
    compression.Level = Albatross.WCF.Extenions.ServiceModel.Channels.CompressionLevel.Normal

    ' If both HTTP and HTTPS endpoints are defined - (in Windows Azure due to load balancing you have the extra dynamic '5105' port)
    ' baseAddresses(0) = 'http://127.0.0.1:5105/RequestProcessor.svc' and baseAddresses(1) = 'https://127.0.0.1:5105/RequestProcessor.svc'

    Dim binding As New Albatross.WCF.Extenions.Bindings.ClearUsernameBinding()
    Dim oUri As System.Uri

    For Each oUri In baseAddresses
      binding = New Albatross.WCF.Extenions.Bindings.ClearUsernameBinding()
      binding.ClearElement.Add(compression)
      Dim oTextMessageEncodingBindingElement As New TextMessageEncodingBindingElement()
      oTextMessageEncodingBindingElement.MessageVersion = MessageVersion.Soap12
      oTextMessageEncodingBindingElement.ReaderQuotas.MaxArrayLength = Integer.MaxValue
      oTextMessageEncodingBindingElement.ReaderQuotas.MaxBytesPerRead = Integer.MaxValue
      oTextMessageEncodingBindingElement.ReaderQuotas.MaxDepth = Integer.MaxValue
      oTextMessageEncodingBindingElement.ReaderQuotas.MaxNameTableCharCount = Integer.MaxValue
      oTextMessageEncodingBindingElement.ReaderQuotas.MaxStringContentLength = Integer.MaxValue

      binding.ClearElement.Add(oTextMessageEncodingBindingElement)
      binding.ClearElement.Add(SecurityBindingElement.CreateUserNameOverTransportBindingElement())

      If InStr(oUri.ToString.ToLower, "https") <> 0 Then
        binding.ClearElement.Add(New HttpsTransportBindingElement) ' https://127.0.0.1/RequestProcessor.svc
      Else
        binding.ClearElement.Add(New Albatross.WCF.Extenions.Bindings.AutoSecuredHttpTransportElement()) ' http://127.0.0.1:9001/RequestProcessor.svc
      End If

      host.AddServiceEndpoint(GetType(Albatross.Common.WCF.IWcfRequestProcessorForDotNetClient), binding, oUri)
    Next

    Return host
  End Function
  Public Overloads Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses As Uri()) As ServiceHostBase
    Return MyBase.CreateServiceHost(constructorString, baseAddresses)
  End Function
End Class

